package com.tengju.bff.interfaces.manage.crm.dashborad.facade;

import com.beust.jcommander.internal.Lists;
import com.google.common.collect.Maps;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecPerformanceObjectiveFormPageQuery;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecTeamPerformanceObjectiveFormPageQuery;
import com.tengju.bff.interfaces.manage.crm.dealer.vo.DealerSupInfoVO;
import com.tengju.bff.interfaces.shared.*;
import com.tengju.bff.interfaces.shared.render.KVAssembler;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.analysis.Meta;
import com.tengju.data.domain.model.analysis.MetaHolder;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.report.performance.PerFormanceOrderType;
import com.tengju.data.domain.model.report.performance.ReportPerformanceRepository;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.shared.CastUtil;
import com.tengju.data.domain.shared.Week;
import com.tengju.motivation.domain.model.performance.PerformanceObjective;
import com.tengju.motivation.domain.model.performance.PerformanceObjectiveObjectId;
import com.tengju.motivation.domain.model.performance.PerformanceObjectiveRepository;
import com.tengju.motivation.domain.model.performance.PerformanceObjectiveTypeEnum;
import com.tengju.motivation.domain.shared.DateType;
import com.tengju.motivation.domain.shared.PerformanceObjectiveType;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.domain.shared.BizException;
import io.sentry.event.User;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 李大仙
 * @Date: 2021/5/6 10:42 上午
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SecPerformanceObjectiveFacade {

    private final ReportPerformanceRepository reportPerformanceRepository;
    private final PerformanceObjectiveRepository performanceObjectiveRepository;
    private final MetaHolder metaHolder;
    private final UserInfoRepository userInfoRepository;

    @Value("#{'${crm.performance.objective.sec}'.split(',')}")
    private String[] performanceObjectives;


    /**
     * sec更新时间
     */
    public KVVO<String,String> getSECTargetUpdateTime(Integer dateType, String date) {
        DateType type = DateType.form(dateType);
        if (type == null) {
            throw new BizException("日期类型未知");
        }
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        LocalDateTime updateTime = null;
        switch (type) {
            case DAY:
                updateTime = reportPerformanceRepository.getUpdateTimeDay(genSecIdCodes,date);
                break;
            case WEEK:
                LocalDate ld = LocalDate.parse(date);
                Week week = new Week(ld);
                updateTime = reportPerformanceRepository.getUpdateTimeWeek(genSecIdCodes,week);
                break;
            case MONTH:
                com.tengju.data.domain.shared.Month month = new com.tengju.data.domain.shared.Month(Integer.valueOf(date));
                updateTime = reportPerformanceRepository.getUpdateTimeMonth(genSecIdCodes,month);
                break;
            case QUARTER:
                com.tengju.data.domain.shared.Quarter quarter = new com.tengju.data.domain.shared.Quarter(date);
                updateTime = reportPerformanceRepository.getUpdateTimeQuarter(genSecIdCodes,quarter);
                break;
            default:
                break;

        }

        return KVVO.of("最近更新时间",
                updateTime == null ? "" :
                        com.tengju.data.domain.shared.DateUtil
                                .formatLocalDateTime(com.tengju.data.domain.shared.DateUtil.DATE_TIME_MINUTE_FORMAT,updateTime));
    }

    public List<KVVO> kaAllTarget(Integer dateType, String date) {
        DateType type = DateType.form(dateType);
        Collection<Meta> metas = metaHolder.listMeta("motivation_target").values();
        LocalDate targetStartDate = null;
        LocalDate targetEndDate = null;
        if (type == null) {
            throw new InterfaceException(InterfaceCode.SYSTEM_ERROR);
        }
        // 获取用户的sec
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        List<Row> rowList = Lists.newArrayList();
        if (DateType.DAY == type) {
            // 天已完成目标
            LocalDate queryDate = LocalDate.parse(date);
            rowList = reportPerformanceRepository.getKaPerformanceDay(genSecIdCodes,date);
            targetStartDate = queryDate;
            targetEndDate = queryDate;
        } else if (DateType.WEEK == type) {
            // 周已完成目标
            LocalDate ld = LocalDate.parse(date);
            Week week = new Week(ld);
            rowList = reportPerformanceRepository.getKaPerformanceWeek(genSecIdCodes,week);
            targetStartDate = week.getStartDate();
            targetEndDate = week.getEndDate();
        } else if (DateType.MONTH == type) {
            // 月已完成目标
            com.tengju.data.domain.shared.Month month = new com.tengju.data.domain.shared.Month(Integer.valueOf(date));
            rowList = reportPerformanceRepository.getKaPerformanceMonth(genSecIdCodes
                    ,month);
            targetStartDate = month.getStartDate();
            targetEndDate = month.getEndDate();
        } else if (DateType.QUARTER == type) {
            // 季度已完成目标
            com.tengju.data.domain.shared.Quarter quarter = new com.tengju.data.domain.shared.Quarter(date);
            rowList = reportPerformanceRepository.getKaPerformanceQuarter(genSecIdCodes,
                    quarter);
            targetStartDate = quarter.getStartDate();
            targetEndDate = quarter.getEndDate();
        }

        // sec配置目标
        List<PerformanceObjective> targetList =
                this.getTargetList(genSecIdCodes,targetStartDate,targetEndDate);

        // 配置目标
        Row row = new Row();
        if (!CollectionUtils.isEmpty(rowList)) {
            row = rowList.get(0);
        }

        // 填充绩效目标
        if (!CollectionUtils.isEmpty(targetList)) {
            for (PerformanceObjective performanceObjective : targetList) {
                BigDecimal existTarget = BigDecimal.valueOf(CastUtil.castDouble(row.getValue(performanceObjective.getKey())));
                row.put(performanceObjective.getKey(),existTarget.add(performanceObjective.getValue()));
            }
        }

        return KVAssembler.buildData(metas,row);
    }


    public TableVO secAllTargetTable(SecPerformanceObjectiveFormPageQuery query,String meta) {
        DateType type = DateType.form(query.getDateType());
        PerFormanceOrderType orderType = PerFormanceOrderType.fromSortType(query.getOrderColumn(), query.getOrderType());
        LocalDate targetStartDate = null;
        LocalDate targetEndDate = null;
        // 导出日期
        String exportDate = "";
        if (type == null) {
            throw new InterfaceException(InterfaceCode.SYSTEM_ERROR);
        }
        // 获取用户的sec
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        List<Row> rowList = Lists.newArrayList();
        int total = 0;

        if (DateType.DAY == type) {
            // 天已完成目标
            LocalDate queryDate = LocalDate.parse(query.getDate());
            rowList = reportPerformanceRepository.getSecAllTargetTableDay(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    query.getDate(),
                    orderType,
                    query.getOffset(),
                    query.getPageSize());

            total = reportPerformanceRepository.getSecAllTargetTableDayTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    query.getDate());

            targetStartDate = queryDate;
            targetEndDate = queryDate;
            exportDate = queryDate.toString();
        } else if (DateType.WEEK == type) {
            // 周已完成目标
            LocalDate ld = LocalDate.parse(query.getDate());
            Week week = new Week(ld);
            rowList = reportPerformanceRepository.getSecAllTargetTableWeek(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    week,
                    orderType,
                    query.getOffset(),
                    query.getPageSize());

            total = reportPerformanceRepository.getSecAllTargetTableWeekTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    week);

            targetStartDate = week.getStartDate();
            targetEndDate = week.getEndDate();
            exportDate = String.format("%s到%s",targetStartDate.toString(),targetEndDate,targetEndDate);
        } else if (DateType.MONTH == type) {
            // 月已完成目标
            com.tengju.data.domain.shared.Month month = new com.tengju.data.domain.shared.Month(Integer.valueOf(query.getDate()));
            rowList = reportPerformanceRepository.getSecAllTargetTableMonth(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    month,
                    orderType,
                    query.getOffset(),
                    query.getPageSize());

            total = reportPerformanceRepository.getSecAllTargetTableMonthTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    month);

            targetStartDate = month.getStartDate();
            targetEndDate = month.getEndDate();
            exportDate = String.format("%s到%s",targetStartDate.toString(),targetEndDate,targetEndDate);
        } else if (DateType.QUARTER == type) {
            // 季度已完成目标
            com.tengju.data.domain.shared.Quarter quarter = new com.tengju.data.domain.shared.Quarter(query.getDate());
            rowList = reportPerformanceRepository.getSecAllTargetTableQuarter(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    quarter,
                    orderType,
                    query.getOffset(),
                    query.getPageSize());

            total = reportPerformanceRepository.getSecAllTargetTableQuarterTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    quarter);

            targetStartDate = quarter.getStartDate();
            targetEndDate = quarter.getEndDate();
            exportDate = String.format("%s到%s",targetStartDate.toString(),targetEndDate,targetEndDate);
        }

        // 用户信息
        Map<Long, UserInfo> userInfoMap = this.getUserInfosToMap(rowList,List.of("bl_sec_id_code"));

        // sec配置目标
        List<PerformanceObjective> targetList = this.getTargetList(genSecIdCodes,targetStartDate,targetEndDate);

        // 填充目标
        this.putTargetValueOfRow(targetList,rowList,"bl_sec_id_code",userInfoMap);

        TableVO tableVO = new TableVO();
        // 设置表头
        TableAssembler.buildColumns(tableVO,metaHolder.listMeta(meta).values());
//        tableVO.addColumn(null,ColumnVO.of("id_code_desc","用户",false));

        // 设置行数据
        TableAssembler.buildData(
                tableVO,
                rowList,
                metaHolder.listMeta(meta).values(),
                List.of("bl_sec_id_code"));

        String resultDate = exportDate;
        tableVO.resolveRow((data) -> {
            // 获取用户昵称+idCode
            Long idCode = CastUtil.castLong(data.get("bl_sec_id_code"));
            UserInfo userInfo = userInfoMap.get(idCode);
            if (userInfo != null) {
                StarLevelEnum starLevelEnum = StarLevelEnum.parseByStarLevel(userInfo.getStarLevel());
                data.put("idCodeDesc",String.format("%s %s(%s)",
                        userInfo.getBackNickName(),
                        starLevelEnum == null ? "" : starLevelEnum.getDesc(),
                        userInfo.getIdCode()));
            }
            data.put("exportDate",resultDate);
        });

        tableVO.setPage(PageResultVO.of(query.getPageIndex(), query.getPageSize(),total));

        return tableVO;
    }

    public TableVO teamAllTargetTable(SecTeamPerformanceObjectiveFormPageQuery query, String meta) {
        DateType type = DateType.form(query.getDateType());
        PerFormanceOrderType orderType = PerFormanceOrderType.fromSortType(query.getOrderColumn(), query.getOrderType());
        LocalDate targetStartDate = null;
        LocalDate targetEndDate = null;
        String exportDate = "";
        if (type == null) {
            throw new InterfaceException(InterfaceCode.SYSTEM_ERROR);
        }
        // 获取用户的sec
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        List<Row> rowList = Lists.newArrayList();
        int total = 0;

        if (DateType.DAY == type) {
            // 天已完成目标
            LocalDate queryDate = LocalDate.parse(query.getDate());
            rowList = reportPerformanceRepository.getSecStarAllTargetTableDay(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    query.getDate(),
                    orderType,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null,
                    query.getOffset(),
                    query.getPageSize()
                    );

            total = reportPerformanceRepository.getSecStarAllTargetTableDayTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    query.getDate(),
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null
            );

            targetStartDate = queryDate;
            targetEndDate = queryDate;
            exportDate = queryDate.toString();
        } else if (DateType.WEEK == type) {
            // 周已完成目标
            LocalDate ld = LocalDate.parse(query.getDate());
            Week week = new Week(ld);
            rowList = reportPerformanceRepository.getSecStarAllTargetTableWeek(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    week,
                    orderType,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null,
                    query.getOffset(),
                    query.getPageSize()
            );

            total = reportPerformanceRepository.getSecStarAllTargetTableWeekTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    week,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null
            );

            targetStartDate = week.getStartDate();
            targetEndDate = week.getEndDate();
            exportDate = String.format("%s到%s",targetStartDate.toString(),targetEndDate.toString());
        } else if (DateType.MONTH == type) {
            // 月已完成目标
            com.tengju.data.domain.shared.Month month = new com.tengju.data.domain.shared.Month(Integer.valueOf(query.getDate()));
            rowList = reportPerformanceRepository.getSecStarAllTargetTableMonth(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    month,
                    orderType,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null,
                    query.getOffset(),
                    query.getPageSize()
            );

            total = reportPerformanceRepository.getSecStarAllTargetTableMonthTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    month,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect())  ? new UserIdCode(query.getSecIdCode()) : null
            );

            targetStartDate = month.getStartDate();
            targetEndDate = month.getEndDate();
            exportDate = String.format("%s到%s",targetStartDate.toString(),targetEndDate.toString());
        } else if (DateType.QUARTER == type) {
            // 季度已完成目标
            com.tengju.data.domain.shared.Quarter quarter = new com.tengju.data.domain.shared.Quarter(query.getDate());
            rowList = reportPerformanceRepository.getSecStarAllTargetTableQuarter(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    quarter,
                    orderType,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null,
                    query.getOffset(),
                    query.getPageSize()
            );

            total = reportPerformanceRepository.getSecStarAllTargetTableQuarterTotal(
                    query.getSecIdCode() == null ? genSecIdCodes : List.of(new UserIdCode(query.getSecIdCode())),
                    quarter,
                    query.getIdCode() == null ? null : new UserIdCode(query.getIdCode()),
                    (query.getIsDirect() != null && query.getIsDirect()) ? new UserIdCode(query.getSecIdCode()) : null
            );

            targetStartDate = quarter.getStartDate();
            targetEndDate = quarter.getEndDate();
            exportDate = String.format("%s到%s",targetStartDate.toString(),targetEndDate.toString());
        }

        // 用户信息
        Map<Long, UserInfo> userInfoMap = this.getUserInfosToMap(rowList,List.of("id_code"));
        // 上级信息
        Map<Long, DealerSupInfoVO> dealerSupInfoVOMap = this.getDealerSupInfosToMap(rowList);

        // 服务商配置目标
        List<PerformanceObjectiveObjectId> objectIdList = rowList.stream()
                .map(v -> CastUtil.castLong(v.getValue("id_code")))
                .filter(v -> v > 0)
                .map(PerformanceObjectiveObjectId::new)
                .collect(Collectors.toList());
        List<PerformanceObjective> targetList = performanceObjectiveRepository.getByObjectIdAndType(
                objectIdList,
                PerformanceObjectiveTypeEnum.SERVICE_PROVIDER,
                com.tengju.data.domain.shared.DateUtil.formatDayInt(targetStartDate),
                com.tengju.data.domain.shared.DateUtil.formatDayInt(targetEndDate));

        // 填充目标
        this.putTargetValueOfRow(targetList,rowList,"id_code",userInfoMap);

        TableVO tableVO = new TableVO();
        // 设置表头
        TableAssembler.buildColumns(tableVO,metaHolder.listMeta(meta).values());
//        tableVO.addColumn(null, ColumnVO.of("id_code_desc", "服务商(ID)", false));


        // 设置行数据
        TableAssembler.buildData(
                tableVO,
                rowList,
                metaHolder.listMeta(meta).values(),
                List.of("id_code"));

        String resultDate = exportDate;
        tableVO.resolveRow((data) -> {
            // 获取用户昵称+idCode
            Long idCode = CastUtil.castLong(data.get("id_code"));
            UserInfo userInfo = userInfoMap.get(idCode);
            if (userInfo != null) {
                StarLevelEnum starLevelEnum = StarLevelEnum.parseByStarLevel(userInfo.getStarLevel());
                data.put("idCodeDesc",String.format("%s %s(%s)",
                        userInfo.getBackNickName(),
                        starLevelEnum == null ? "" : starLevelEnum.getDesc(),
                        userInfo.getIdCode()));
            }
            // 上级信息
            DealerSupInfoVO dealerSupInfoVO = dealerSupInfoVOMap.get(idCode);
            if (dealerSupInfoVO != null) {
                data.put("blSuper",dealerSupInfoVO);
            }

            data.put("exportDate",resultDate);
        });

        tableVO.setPage(PageResultVO.of(query.getPageIndex(), query.getPageSize(),total));

        return tableVO;
    }


    /**
     *  获取上级信息
     */
    private Map<Long, DealerSupInfoVO> getDealerSupInfosToMap(List<Row> rowList) {
        Map<Long,DealerSupInfoVO> supInfoMap = Maps.newHashMap();
        if (CollectionUtils.isEmpty(rowList)) {
            return supInfoMap;
        }
        for (Row row : rowList) {
            DealerSupInfoVO dealerSupInfoVO = new DealerSupInfoVO();
            dealerSupInfoVO.setSupUserName(String.format("上级：%s%s",CastUtil.castString(row.getValue("sup_user_name")),
                    row.getValue("sup_id_code") == null ? "":"("+row.getValue("sup_id_code")+")"));
            dealerSupInfoVO.setAsscHkName(String.format("所属黑卡：%s%s",CastUtil.castString(row.getValue("assc_hk_name")),
                    row.getValue("assc_hk_id_code") == null ? "":"("+row.getValue("assc_hk_id_code")+")"));
            dealerSupInfoVO.setBlStar1Name(String.format("所属1星：%s%s",CastUtil.castString(row.getValue("bl_star1_name")),
                    row.getValue("bl_star1_id_code") == null ? "":"("+row.getValue("bl_star1_id_code")+")"
            ));
            dealerSupInfoVO.setBlStar2Name(String.format("所属2星：%s%s",CastUtil.castString(row.getValue("bl_star2_name")),
                    row.getValue("bl_star2_id_code") == null ? "":"("+row.getValue("bl_star2_id_code")+")"
            ));
            dealerSupInfoVO.setBlStar3Name(String.format("所属3星：%s%s",CastUtil.castString(row.getValue("bl_star3_name")),
                    row.getValue("bl_star3_id_code") == null ? "":"("+row.getValue("bl_star3_id_code")+")"
            ));
            dealerSupInfoVO.setBlStar4Name(String.format("所属4星：%s%s",CastUtil.castString(row.getValue("bl_star4_name")),
                    row.getValue("bl_star4_id_code") == null ? "":"("+row.getValue("bl_star4_id_code")+")"
            ));
            dealerSupInfoVO.setBlEcName(String.format("所属EC：%s%s",CastUtil.castString(row.getValue("bl_ec_name")),
                    row.getValue("bl_ec_id_code") == null ? "":"("+row.getValue("bl_ec_id_code")+")"
            ));
            dealerSupInfoVO.setBlSecName(String.format("所属SEC：%s%s",CastUtil.castString(row.getValue("bl_sec_name")),
                    row.getValue("bl_sec_id_code") == null ? "":"("+row.getValue("bl_sec_id_code")+")"
            ));
            dealerSupInfoVO.setBlClubName(String.format("所属俱乐部：%s%s",CastUtil.castString(row.getValue("bl_club_name")),
                    row.getValue("bl_club_id") == null ? "":"("+row.getValue("bl_club_id")+")"
            ));
            supInfoMap.put(CastUtil.castLong(row.getValue("id_code")),dealerSupInfoVO);
        }

        return supInfoMap;
    }


    /**
     * 获取用户信息
     */
    private Map<Long, UserInfo> getUserInfosToMap(List<Row> rowList, List<String> keyList) {
        if (CollectionUtils.isEmpty(rowList)) {
            return Maps.newHashMap();
        }
        if (CollectionUtils.isEmpty(keyList)) {
            return Maps.newHashMap();
        }

        List<Long> idCodeList = com.google.common.collect.Lists.newArrayList();
        rowList.forEach(row -> keyList.forEach(key -> idCodeList.add(CastUtil.castLong(row.getValue(key)))));

        List<UserIdCode> userIdCodeList= idCodeList.stream()
                .filter(v -> v > 0L)
                .map(UserIdCode::new)
                .distinct()
                .collect(Collectors.toList());
        List<UserInfo> userInfos = userInfoRepository.batchGetUserInfo(userIdCodeList);

        return userInfos.stream()
                .collect(Collectors.toMap(k -> k.getIdCode().getValue(), v -> v, (v1, v2) -> v1));
    }


    /**
     * 获取绩效目标
     */
    private List<PerformanceObjective> getTargetList(List<UserIdCode> genSecIdCodes,
                                                     LocalDate targetStartDate,
                                                     LocalDate targetEndDate) {

        if (CollectionUtils.isEmpty(genSecIdCodes)) {
            return Collections.emptyList();
        }
        List<PerformanceObjectiveObjectId> objectIdList = genSecIdCodes.stream()
                .map(v -> new PerformanceObjectiveObjectId(v.getValue()))
                .collect(Collectors.toList());

        return performanceObjectiveRepository.getByObjectIdAndType(
                objectIdList,
                PerformanceObjectiveTypeEnum.GEN_SEC,
                com.tengju.data.domain.shared.DateUtil.formatDayInt(targetStartDate),
                com.tengju.data.domain.shared.DateUtil.formatDayInt(targetEndDate));

    }

    private void putTargetValueOfRow(List<PerformanceObjective> targetList,
                                     List<Row> performanceRows,
                                     String key,
                                     Map<Long, UserInfo> userInfoMap) {

        Map<Long,List<PerformanceObjective>> targetMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(targetList)) {
            for (PerformanceObjective performanceObjective : targetList) {
                long idCodeVal = performanceObjective.getObjectId().getValue();
                List<PerformanceObjective> idCodeTargetList = targetMap.getOrDefault(idCodeVal,new ArrayList<>());
                idCodeTargetList.add(performanceObjective);
                targetMap.put(idCodeVal,idCodeTargetList);
            }
        }

        // 填充绩效目标
        for (Row performanceRow : performanceRows) {
            Long idCodeVal = CastUtil.castLong(performanceRow.getValue(key));
            List<PerformanceObjective> idCodeTargetList = targetMap.getOrDefault(idCodeVal,new ArrayList<>());
            for (PerformanceObjective performanceObjective : idCodeTargetList) {
                BigDecimal existTarget = BigDecimal.valueOf(CastUtil.castDouble(performanceRow.getValue(performanceObjective.getKey())));
                performanceRow.put(performanceObjective.getKey(),existTarget.add(performanceObjective.getValue()));
            }

            // 用户等级、昵称
            UserInfo userInfo = userInfoMap.get(idCodeVal);
            if (userInfo != null) {
                performanceRow.put("user_nick_name",userInfo.getBackNickName());
                //等级
                StarLevelEnum starLevelEnum = StarLevelEnum.parseByStarLevel(userInfo.getStarLevel());
                if (starLevelEnum != null) {
                    performanceRow.put("user_level_desc",starLevelEnum.getDesc());
                }

            }

        }

    }



}

